import Layout from '@/layout/index.vue';
import type { AppRoute } from '@/types/route';
import { createRouter, createWebHistory } from 'vue-router';
import appsRouter from './modules/apps';
import authorRouter from './modules/author';
import blogCreateRouter from './modules/blogCreate';
import blogEditRouter from './modules/blogEdit';
import blogGanttRouter from './modules/blogGantt';
import blogListRouter from './modules/blogList';
import blogViewRouter from './modules/blogView';
import missionRouter from './modules/mission';
import noticeRouter from './modules/notice';
import specialColumnRouter from './modules/specialColumn';
import systemRouter from './modules/system';
import wallRouter from './modules/wall';

// 异步组件
export const asyncRoutes = [
  ...blogViewRouter,
  ...blogListRouter,
  ...blogGanttRouter,
  ...blogCreateRouter,
  ...blogEditRouter,
  ...noticeRouter,
  ...specialColumnRouter,
  ...wallRouter,
  ...authorRouter,
  ...appsRouter,
  ...systemRouter,
  ...missionRouter
];

/**
 * path ==> 路由路径
 * name ==> 路由名称
 * component ==> 路由组件
 * redirect ==> 路由重定向
 * alwaysShow ==> 如果设置为true，将始终显示根菜单，无论其子路由长度如何
 * hidden ==> 如果"hidden:true"不会显示在侧边栏中（默认值为false）
 * keepAlive ==> 设为true 缓存
 * meta ==> 路由元信息
 * meta.title ==> 路由标题
 * meta.icon ==> 菜单icon
 * meta.iconShow ==> 菜单icon显示 --------------------------------------------- 新增属性 2024-10-10
 * meta.affix ==> 如果设置为true将会出现在 标签栏中
 * meta.breadcrumb ==> 如果设置为false，该项将隐藏在breadcrumb中（默认值为true）
 * meta.activeMenu ==> 详情页的时候可以设置菜单高亮 ,高亮菜单的path
 * meta.watermark ==> 是否在当前路由显示水印（默认为false，不显示）
 */

export const constantRoutes: AppRoute[] = [
  {
    path: '/404',
    name: '404',
    component: () => import('@/views/errorPages/404.vue'),
    hidden: true
  },
  {
    path: '/403',
    name: '403',
    component: () => import('@/views/errorPages/403.vue'),
    hidden: true
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login/index.vue'),
    hidden: true,
    meta: { title: '登录页' }
  },
  {
    path: '/login/qq/callback',
    name: 'QQCallback',
    component: () => import('@/views/login/LoginCallback.vue'),
    hidden: true,
    meta: { title: 'QQ授权页' }
  },
  {
    path: '/login/github/callback',
    name: 'GithubCallback',
    component: () => import('@/views/login/LoginCallback.vue'),
    hidden: true,
    meta: { title: 'GitHub授权页' }
  },
  {
    path: '/',
    name: 'layout',
    component: Layout,
    redirect: '/home',
    meta: {
      title: '首页',
      icon: 'House'
    },
    children: [
      {
        path: '/home',
        component: () => import('@/views/home/index.vue'),
        name: 'home',
        meta: {
          title: 'MonkeyK',
          icon: 'House',
          affix: true,
          role: ['GUEST', 'USER']
        }
      }
    ]
  }
];

/**
 * notFoundRouter(找不到路由)
 */
export const notFoundRouter = {
  path: '/:pathMatch(.*)',
  name: 'notFound',
  redirect: '/404'
};

const router = createRouter({
  history: createWebHistory(import.meta.env.VITE_BASE_URL), // history
  routes: constantRoutes
});

/**
 * 重置路由
 */
export function resetRouter() {
  // 仅移除动态路由，保留 constantRoutes
  try {
    // 收集常量路由的所有 name（含子路由）
    const constantNames = new Set<string>();
    const collectNames = (routes: AppRoute[]) => {
      routes.forEach(r => {
        if (r.name) constantNames.add(String(r.name));
        if (Array.isArray(r.children) && r.children.length) {
          collectNames(r.children);
        }
      });
    };
    collectNames(constantRoutes);

    router.getRoutes().forEach(route => {
      const { name } = route;
      if (!name) return; // 必须有 name 才能移除
      const nameStr = String(name);
      if (!constantNames.has(nameStr) && router.hasRoute(name)) {
        router.removeRoute(name);
      }
    });
  } catch (error) {
    // 强制刷新浏览器，不推荐
    window.location.reload();
  }
}

export default router;
